/**
 * Copyright 2011 HNB. All rights reserved.
 * HNB PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package my.hnb.android.view.dialog;

import java.util.ArrayList;

import my.hnb.android.dto.OptionItemDTO;
import my.hnb.android.view.control.CustomDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.widget.ListView;

/**
 * Dialog hien thi the loai nhac
 * @author BANGHN
 */
public class OptionDialog implements DialogInterface, OnKeyListener {
	// dialog view
	//protected View dialogView = null;
	// context cua activity chua dialog
	protected Context context = null;
	// alert dialog
	protected CustomDialog dialog = null;
	protected OnCancelListener cancelListener = null;
	public boolean isShowing = false;
	ListView lvOption;
	String title;
	Drawable icon;
	
	public OptionDialog(Context context) {
		this.context = context;
	}
	public OptionDialog(Context context, String title, Drawable icon) {
		this.context = context;
		this.title = title;
		this.icon = icon;
	}

	OptionDialogView dialogView;
	public void showDialog(ArrayList<OptionItemDTO> arrayOption, int action) {
		//if (dialog == null) {
			CustomDialog.Builder builder = new CustomDialog.Builder(context);
			dialogView = new OptionDialogView(context, arrayOption);
			dialogView.setAction(action);
			dialogView.setTileAndIcon(title, icon);
			builder.setContentView(dialogView.dialogView);
			dialog = builder.create();
		//}
		isShowing = true;
		dialog.setOnKeyListener(this);
		dialog.show();
	}

	
	@Override
	public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			isShowing = false;
			if (cancelListener != null) {
				cancelListener.onCancel(this);
			}
		}
		return false;
	}

	@Override
	public void cancel() {
		if (dialog != null) {
			dialog.cancel();
			isShowing = false;
		}
	}

	@Override
	public void dismiss() {
		if (dialog != null) {
			dialog.dismiss();
			isShowing = false;
		}
	}

}
